<?php
/**
 * @package techniv-bb.tcnv-fw
 * @version 0.0.1
 * @copyright © Techniv 2011
 * @license <http://opensource.org/licenses/gpl-3.0.html> GNU General Public License - 3.0
 */

/**
 * Description of TcnvListner
 *
 * @author Vincent
 */
class TcnvListner {

	
	/** @var TcnvInterceptor */
	public static $interceptors;
	
	


	/**
	 * Inject action before the block_var assignement.
	 * <i>Call at begin of the Template::assign_block_vars(), arround 
	 * line 472 of /includes/template.php</i>
	 * @param string $block_name The name of block.
	 * @param mixed $vars The associating array of assigned variables.
	 */
	public static function tpl_assign_block($block_name, &$vars) {
		$args = array(
			'block_name'	=> $block_name, 
			'vars'			=> &$vars
		);
		self::fire_interceptor(InterceptBlocAssignement::$_name, $args);
	}
	
	/**
	 * Inject action before the template vars assignement.<br>
	 * <i>Call at begin of the Template::assign_vars(), arround 
	 * line 446 of /includes/template.php</i>
	 * @param type $vars_array 
	 */
	public static function tpl_assign_vars(&$vars_array){
		$args = array(
			'vars'	=> &$vars_array
		);
		self::fire_interceptor(InterceptVarsAssignement::$_name, $args);
	}

		/**
		 * Inject action before display render by template engine.
		 * <i>Call at begin of Template::display(), arround line 204
		 * of /includes/template.php</i> 
		 * @param template $template
		 */
	public static function display(&$template,&$handle){
		TcnvTemplateSync::put_tpl_var(&$template);
		
		$args = array(
			'template'	=> &$template,
			'handle'	=> &$handle,
		);
		self::fire_interceptor(InterceptDisplay::$_name, &$args);
	}
	
	/**
	 * Inject action for each post row loaded. Intercept the database datas.
	 * <i>Call just before insert the datas in template engine. Arround line 1583
	 * of /viewtopic.php</i>
	 * @param array $row The row of database result.
	 */
	public static function load_postrow(&$postrow){
		if(!isset(self::$interceptors[InterceptPostRow::$_name])) return;
		$interceptor = self::$interceptors[InterceptPostRow::$_name];
		$interceptor instanceof TcnvInterceptor;
		$interceptor->fire_intercept(&$postrow);
	}
	
	/**
	 * Inject action before the post persist.
	 * @param string $post_mode Identify the posting mode :<table>
	 * <tr><td>post</td><td>post of new topic</td></tr>
	 * <tr><td>reply</td><td>post a reply to an existing topic</td></tr>
	 * <tr><td>edit(_[a-z]*)?</td><td>editing mode</td></tr>
	 * @param string $poster_id Is the id number of user witch post.
	 * @param mixed $sql_data Is a bidimentionnal array that contain all datas that will be
	 * persisted.
	 */
	public static function posting_before_persist(&$post_mode, &$poster_id, &$sql_data){
		$args = array(
			'post_mode'		=> &$post_mode,
			'poster_id'		=> &$poster_id,
			'data'			=> &$sql_data
		);
		if(!isset(self::$interceptors[InterceptPosting::$_name])){
			return;
		}
		
		$interceptor = self::$interceptors[InterceptPosting::$_name];
		$interceptor instanceof TcnvInterceptor;
		
		$interceptor->fire_intercept(&$args,'before');
	}
	
	/**
	 * Inject action after the post persist.
	 * @param boolean $error
	 * @param string $mode
	 * @param mixed $data
	 * @param string $url 
	 */
	public static function posting_after_persist($post_mode, $poster_id, &$data, &$url){
		if(!isset(self::$interceptors[InterceptPosting::$_name])) return;
		
		$args = array(
			'post_mode'	=> $post_mode,
			'poster_id'	=> $poster_id,
			'data'		=> &$data,
			'url'		=> &$url,
		);
		
		$interceptor = self::$interceptors[InterceptPosting::$_name];
		$interceptor instanceof TcnvInterceptor;
		
		$interceptor->fire_intercept(&$args,'after');
	}

	/**
	 * Intercept the message just before or after is parsing by phpBB message parser.
	 * @param string $position A string to indicate if intercept before or after parsing.
	 * @param string $message Message.
	 * @param bolean $allow_bbcode Define if bbCode is allow.
	 * @param bolean $allow_magic_url Define if allow the magic url parsing.
	 * @param boolean $allow_smilies Define if smilies are allow.
	 */
	public static function parseDisplayMassage($position,
			&$message,&$allow_bbcode,&$allow_magic_url,&$allow_smilies) {
		
		$args = array(
			'message' => &$message,
			'allow_bbcode' => &$allow_bbcode,
			'allow_magic_url' => &$allow_magic_url,
			'allow_smilies' => &$allow_smilies
		);
		
		$interceptor = self::$interceptors[InterceptParseDisplayMessage::$_name];
		$interceptor instanceof TcnvInterceptor;
		
		$interceptor->fire_intercept(&$args, $position);
	}

	public static function add_intercepting($interceptor_name, $intercepting){
		if(!isset(self::$interceptors[$interceptor_name])){
			self::$interceptors[$interceptor_name] = new TcnvInterceptor($interceptor_name);
		}
		
		$interceptor = self::$interceptors[$interceptor_name];
		$interceptor instanceof TcnvInterceptor;
		$interceptor->add_intercepting_methods($intercepting);
	}
	
	/** @return TcnvInterceptor */
	private static function get_interceptor($name){
		if(!isset(self::$interceptors[$name])) return NULL;
		$interceptor = self::$interceptors[$name];
		$interceptor instanceof TcnvInterceptor;
		return $interceptor;
	}
	
	/**
	 *
	 * @param string $name The name of interceptor launch.
	 * @param array $args The argument to pass to mod.
	 * @param type $condition (optional) The execution contition.
	 * @return type 
	 */
	private static function fire_interceptor($name, &$args, $condition = null){
		$interceptor = self::get_interceptor($name);
		if(!isset ($interceptor)) return null;
		$interceptor instanceof TcnvInterceptor;
		
		$interceptor->fire_intercept(&$args, $condition);
	}
}

?>
